package com.example.longimageView.db;

import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.example.longimageView.App;

import org.xutils.DbManager;
import org.xutils.common.util.KeyValue;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;

/**
 * @author: njb
 * @date: 2023/5/10 23:20
 * @desc:
 */
public class UserDbManager <T>{
    private final String TAG = "SerialPortWifiDbManager";

    private DbManager mDbManager;

    private UserDbManager() {
        mDbManager = App.getInstance().getLocalDbManager();
    }


    private static class SingletonContainer {
        private static UserDbManager instance = new UserDbManager();
    }


    public static UserDbManager getInstance() {
        return UserDbManager.SingletonContainer.instance;
    }

    /**
     * 保存通用无线设备
     *
     * @param UserDataBaseBean 传入设备信息
     */
    public void saveDevice(UserDataBaseBean UserDataBaseBean) {
        try {
            LogUtils.i(TAG, "=====saveWireLessDevice: " + UserDataBaseBean);
            mDbManager.saveOrUpdate(UserDataBaseBean);
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "====saveWireLessDevice: 出错！！ " + getExceptionInfo(e));
        }
    }


    /**
     * 通过deviceName删除设备
     *
     * @param deviceName return 删除条数 -1则异常
     */
    public int deleteByDeviceName(String deviceName) {
        try {
            int count = mDbManager.delete(UserDataBaseBean.class, WhereBuilder.b().and("deviceName", "=", deviceName));
            return count;
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "deleteByDeviceName: " + getExceptionInfo(e));
            return -1;
        }
    }

    /**
     * 通过applianceId删除设备
     *
     * @param applianceId
     * @return 删除条数 -1则异常
     */
    public int deleteByApplianceId(int applianceId) {
        try {
            int count = mDbManager.delete(UserDataBaseBean.class, WhereBuilder.b().and("deviceName", "=", applianceId));
            return count;
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "deleteByRunName: " + getExceptionInfo(e));
            return -1;
        }
    }


    /**
     * 删除所有设备-删除表
     */
    public void deleteAllDevices() {
        try {
            mDbManager.delete(UserDataBaseBean.class);
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "deleteAllDevices: " + getExceptionInfo(e));
        }
    }


    /**
     * 更新设备状态
     *
     * @param deviceName
     * @return 修改多少条数
     */
    public int updateByDeviceName(String deviceName, KeyValue... kv) {
        try {
            int s = mDbManager.update(UserDataBaseBean.class, WhereBuilder.b().and("deviceName", "=", deviceName), kv);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(TAG, "updateByDeviceName: " + getExceptionInfo(e));
            return -1;
        }
    }

    /**
     * ApplianceId更新设备状态
     *
     * @param id
     * @return 修改多少条数
     */
    public int updateByApplianceId(String id, KeyValue... kv) {
        try {
            int s = mDbManager.update(UserDataBaseBean.class, WhereBuilder.b().and("id", "=", id), kv);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(TAG, "updateByApplianceId: " + getExceptionInfo(e));
            return -1;
        }
    }

    /**
     * 更新设备状态
     *
     * @param UserDataBaseBean 传入结构体更新数据库
     */
    public void updateDevice(UserDataBaseBean UserDataBaseBean) {
        try {
            mDbManager.replace(UserDataBaseBean);
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "updateDevice: " + getExceptionInfo(e));
        }
    }

    /**
     * 更新设备状态
     *
     * @param
     * @return 修改多少条数
     */
    public int updateDevices(Class<T> tClass, String keyName, String value, KeyValue... kv) {
        try {
            int s = mDbManager.update(tClass, WhereBuilder.b().and(keyName, "=", value), kv);
            return s;
        } catch (DbException e) {
            e.printStackTrace();
            Log.e(TAG, "updateByDeviceName: " + getExceptionInfo(e));
            return -1;
        }
    }


    /**
     * 获取所有设备
     *
     * @return List<WiFiDeviceDataBaseBean>
     */
    public List<UserDataBaseBean> getAllDevices() {
        try {
            List<UserDataBaseBean> UserDataBaseBeanList = mDbManager.findAll(UserDataBaseBean.class);
            return UserDataBaseBeanList;
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "getAllDevices: " + getExceptionInfo(e));
            return null;
        }
    }

    /**
     * 根据设备名称查找设备
     *
     * @param name
     * @return WiFiDeviceDataBaseBean
     */
    public UserDataBaseBean queryDeviceByName(String name) {
        try {
            UserDataBaseBean userDataBaseBean = mDbManager.selector(UserDataBaseBean.class).where("name", "=", name).findFirst();
            return userDataBaseBean;
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "queryDeviceByName: " + getExceptionInfo(e));
            return null;
        }
    }

    /**
     * 根据设备类型查找设备
     *
     * @param type 根据接口获取的设备类型
     * @return WiFiDeviceDataBaseBean   deviceType
     */
    public List<UserDataBaseBean> queryDeviceByDeviceType(int type) {
        try {
            List<UserDataBaseBean> device = mDbManager.selector(UserDataBaseBean.class).where("devicesType", "=", type).findAll();
            return device;
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "queryDeviceByName: " + getExceptionInfo(e));
            return null;
        }
    }

    /**
     * 根据applianceId查找设备
     *
     * @param id : applianceId
     * @return WiFiDeviceDataBaseBean
     */
    public UserDataBaseBean queryDeviceByApplianceId(String id) {
        try {
            UserDataBaseBean device = mDbManager.selector(UserDataBaseBean.class).where("id", "=", id).findFirst();
            return device;
        } catch (DbException e) {
            e.printStackTrace();
            LogUtils.e(TAG, "queryDeviceByApplianceId: " + getExceptionInfo(e));
            return null;
        }
    }

    public String getExceptionInfo(Exception e) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        return baos.toString();
    }
}
